/*
 * Copyright (c) 2023 fred
 * Licensed under the Apache License,Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
import List from '@ohos.util.List';

export class Validate {

  /**
   * Validates that the object is not null
   * @param obj object to test
   * @param msg message to output if validation fails
   */
  public static notNull(obj: any, msg: string = "Object must not be null") {
    if (obj === undefined || obj === null) {
      throw new Error(msg);
    }
  }

  /**
   Validates that the parameter is not null

   * @param obj the parameter to test
   * @param param the name of the parameter, for presentation in the validation exception.
   * @throws ValidationException if the object is null
   */
  public static notNullParam(obj:any, param:string) {
    if (obj === undefined || obj === null){
      throw new Error(`The parameter '${param}' must not be null.`);
    }
  }

  public static ensureNotNull(obj: any, msg: string = "Object must not be null"):any {
    if (obj === undefined || obj === null) {
      throw new Error(msg);
    }
    return obj
  }

  /**
   * Validates that the value is true
   * @param val object to test
   * @param msg message to output if validation fails
   */
  public static isTrue(val: boolean, msg: string = "Must be true") {
    if (!val) {
      throw new Error(msg);
    }
  }

  /**
   * Validates that the value is false
   * @param val object to test
   * @param msg message to output if validation fails
   */
  public static isFalse(val: boolean, msg: string = "Must be false") {
    if (val) {
      throw new Error(msg);
    }
  }

  /**
   * Validates that the array contains no null elements
   * @param objects the array to test
   * @param msg message to output if validation fails
   */
  public static noNullElements(objects: any[], msg: string = "Array must not contain any null objects") {
    for (let index = 0; index < objects.length; index++) {
      const element = objects[index];
      if (element === undefined || element === null) {
        throw new Error(msg);
      }
    }
  }

  public static noNullElements1(objects: List<any>, msg: string = "Array must not contain any null objects") {
    for (let index = 0; index < objects.length; index++) {
      const element = objects[index];
      if (element === undefined || element === null) {
        throw new Error(msg);
      }
    }
  }

  /**
   * Validates that the string is not empty
   * @param string the string to test
   * @param msg message to output if validation fails
   */
  public static notEmpty(string: string|String, msg: string = "String must not be empty") {
    if (string === undefined || string === null /*|| string.length === 0*/) {
      throw new Error(msg);
    }
  }

  public static notEmptyParam(string: string,parm:string) {
    if (string === undefined || string === null || string.length === 0) {
      throw new Error(`The ${parm} parameter must not be empty.`);
    }
  }

  /**
   * Blow up if we reach an unexpected state.
   * @param msg message to think about
   */
  public static wtf(msg: string) {
    throw new Error(msg);
  }
  /**
   Cause a failure.
   @param msg message to output.
   */
  public static fail(msg: string) {
    throw new Error(msg);
  }
}
